package com.mcxx.convert;

import com.mcxx.modules.miniSalvation.family.entity.*;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.miniSalvation.family.dto.FamilyDTO;
import com.mcxx.modules.miniSalvation.salary.entity.FamilySalaryEntity;
import com.mcxx.modules.miniSalvation.salary.entity.MemberSalaryEntity;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMainEntity;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMainHistoryEntity;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberEntity;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberHistoryEntity;
import com.mcxx.modules.temporary.order.entity.TmpOrderEntity;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import org.springframework.beans.BeanUtils;

import java.text.ParseException;
import java.util.Date;

public class FamilyConvert {

    public static FamilyMainEntity convert(FamilyDTO familyDTO) {
        FamilyMainEntity familyMainEntity = new FamilyMainEntity();
        String id = Constant.getUUID();
        if (familyDTO.getFamilyId() != null) {
            id = familyDTO.getFamilyId();
        }
        familyMainEntity.setId(id);
        familyMainEntity.setName(familyDTO.getApplyName());
        familyMainEntity.setIdcard(familyDTO.getIdcard());
        familyMainEntity.setPhone(familyDTO.getPhone());
        familyMainEntity.setAreaCode(familyDTO.getAddressCode());
        familyMainEntity.setAddress(familyDTO.getResidentialAddress());
        familyMainEntity.setSaveMemberNumber(familyDTO.getSaveMemberNumber());
        familyMainEntity.setMainReason(familyDTO.getMainlyPovertyReasons());
        familyMainEntity.setFamilyObjectType(familyDTO.getObjectType());
        familyMainEntity.setRemark(familyDTO.getRemark());
        familyMainEntity.setSaveType(familyDTO.getCalculationMethod());
        familyMainEntity.setCreateTime(familyDTO.getApplyDate());

        return familyMainEntity;
    }




    public static BankEntity convertBank(FamilyDTO familyDTO) {
        BankEntity bankEntity = new BankEntity();
        bankEntity.setId(Constant.getUUID());
        if (!CommonUtil.invalidArgs(familyDTO.getFamilyMemberId())) {
            bankEntity.setMemberId(familyDTO.getFamilyMemberId());
        }
        bankEntity.setAgentAccount(familyDTO.getBankAccount());
        bankEntity.setAgentOperator(familyDTO.getAccountHolderName());
        bankEntity.setAgentAccountIdcard(familyDTO.getBankAccountIdCard());
        bankEntity.setAgentMemberAddress(familyDTO.getBankAccountAddress());
        bankEntity.setAgentBankCode(familyDTO.getOpenAccountBank());
        bankEntity.setPayType(familyDTO.getDisbursement());
        bankEntity.setBusinessType(familyDTO.getBusinessCode());
        bankEntity.setStatus(Constant.BankStatus.OK);
        bankEntity.setAgentBankPhone(familyDTO.getBankPhone());
        bankEntity.setAgentRelation(familyDTO.getRelation());
        return bankEntity;
    }


    public static FamilyDTO convertFromBank(BankEntity bankEntity) {
        FamilyDTO familyDTO = new FamilyDTO();
        familyDTO.setAccountHolderName(bankEntity.getAgentOperator());
        familyDTO.setBankAccount(bankEntity.getAgentAccount());
        familyDTO.setOpenAccountBank(bankEntity.getAgentBankCode());
        familyDTO.setDisbursement(bankEntity.getPayType());
        familyDTO.setBankAccountIdCard(bankEntity.getAgentAccountIdcard());
        familyDTO.setBankAccountAddress(bankEntity.getAgentMemberAddress());
        familyDTO.setBankPhone(bankEntity.getAgentBankPhone());
        familyDTO.setRelation(bankEntity.getAgentRelation());
        return familyDTO;
    }


    public static MemberSalaryEntity convertFamilyMemberSalary(FamilyDTO familyDTO) {
        MemberSalaryEntity memberEntity = new MemberSalaryEntity();
        memberEntity.setId(Constant.getUUID());
        memberEntity.setBusinessType(familyDTO.getBusinessCode());

        if (familyDTO.getSalvMoney() != null && !"".equals(familyDTO.getSalvMoney())) {
            memberEntity.setSalaryNumber(familyDTO.getSalvMoney());
        }
        if (familyDTO.getAddressCode() != null && !"".equals(familyDTO.getAddressCode())) {
            memberEntity.setAreaCode(familyDTO.getAddressCode());
            memberEntity.setCountyCode(familyDTO.getAddressCode().substring(0, 6));
        }
        memberEntity.setSalaryType(Constant.SalaryType.ZHENGCHANG);
        if (familyDTO.getBusinessCode().equals(Constant.BusinessCode.LINSHI)) {
            try {
                memberEntity.setEndDate(DateUtil.sdf2.parse(familyDTO.getRescueYears()));
                memberEntity.setStartDate(DateUtil.sdf2.parse(familyDTO.getRescueYears()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (familyDTO.getBusinessCode().equals(Constant.BusinessCode.DIBAO) || familyDTO.getBusinessCode().equals(Constant.BusinessCode.TEKUN)) {
            memberEntity.setEndDate(Constant.MAX_YEAR_DATE);
        }
        memberEntity.setMemberId(familyDTO.getFamilyMemberId());
        memberEntity.setFamilyId(familyDTO.getFamilyId());
        memberEntity.setOrderId(familyDTO.getOrderId());
        memberEntity.setBusinessType(familyDTO.getBusinessCode());
        return memberEntity;
    }


    public static FamilySalaryEntity convertFamilySalary(FamilyDTO familyDTO) {
        FamilySalaryEntity familyEntity = new FamilySalaryEntity();
        familyEntity.setId(Constant.getUUID());
        familyEntity.setBusinessType(familyDTO.getBusinessCode());

        if (familyDTO.getSalvMoney() != null && !"".equals(familyDTO.getSalvMoney())) {
            familyEntity.setSalaryNumber(familyDTO.getSalvMoney());
        }
        if (familyDTO.getAddressCode() != null && !"".equals(familyDTO.getAddressCode())) {
            familyEntity.setAreaCode(familyDTO.getAddressCode());
            familyEntity.setCountyCode(familyDTO.getAddressCode().substring(0, 6));
        }

        familyEntity.setSalaryType(Constant.SalaryType.ZHENGCHANG);
        familyEntity.setBusinessType(familyDTO.getBusinessCode());

        if (familyDTO.getBusinessCode().equals(Constant.BusinessCode.LINSHI)) {
            try {
                familyEntity.setEndDate(DateUtil.sdf2.parse(familyDTO.getRescueYears()));
                familyEntity.setStartDate(DateUtil.sdf2.parse(familyDTO.getRescueYears()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (familyDTO.getBusinessCode().equals(Constant.BusinessCode.DIBAO) || familyDTO.getBusinessCode().equals(Constant.BusinessCode.TEKUN)) {
            familyEntity.setEndDate(Constant.MAX_YEAR_DATE);
        }

        if (!CommonUtil.invalidArgs(familyDTO.getObjectType())) {
            familyEntity.setSupplyType(familyDTO.getObjectType());
        }
        if (!CommonUtil.invalidArgs(familyDTO.getSupplyOrg())) {
            familyEntity.setSupplyOrgId(familyDTO.getSupplyOrg());
        }
        familyEntity.setFamilyId(familyDTO.getFamilyId());
        familyEntity.setOrderId(familyDTO.getOrderId());
        return familyEntity;
    }


    public static FamilyMainHistoryEntity convertFamilyMainHistoryEntity(OrderEntity orderEntity, FamilyMainEntity familyMainEntity,
                                                                         String rescueYears,  String salvationNo,
                                                                         UserAccountVo userAccountVo){
        FamilyMainHistoryEntity entity = new FamilyMainHistoryEntity();

        BeanUtils.copyProperties(familyMainEntity, entity);

        entity.setId(Constant.getUUID());

        entity.setSaveType(orderEntity.getCountType());

        entity.setOrderId(orderEntity.getOrderId());

        entity.setFamilyId(familyMainEntity.getId());

        entity.setSalvationNo(salvationNo);

        entity.setFristStartDate(rescueYears);

        entity.setOrgId(userAccountVo.getOrgId());

        entity.setUpdateBy(userAccountVo.getUserId());

        entity.setUpdateTime(new Date());

        entity.setCreateBy(userAccountVo.getUserId());

        entity.setCreateTime(new Date());

        entity.setMasterInSave(Constant.IsSaveObject.YES);

        return entity;
    }


    public static FamilyMemberHistoryEntity convertFamilyMemberHistoryEntity(OrderEntity orderEntity,
                                                                             FamilyMemberEntity familyMemberEntity, FamilyMainEntity familyMainEntity,
                                                                             UserAccountVo userAccountVo){
        FamilyMemberHistoryEntity entity = new FamilyMemberHistoryEntity();

        BeanUtils.copyProperties(familyMemberEntity, entity);

        entity.setId(Constant.getUUID());

        entity.setOrderId(orderEntity.getOrderId());

        entity.setFamilyId(orderEntity.getFamilyId());

        entity.setAddress(familyMainEntity.getAddress());

        entity.setMemberId(familyMemberEntity.getId());

        entity.setCreateBy(userAccountVo.getUserId());

        entity.setCreateTime(new Date());

        return entity;
    }


    public static FamilySpecialHistoryEntity convertFamilySpecialHistoryEntity(OrderEntity orderEntity,
                                                                              FamilySpecialEntity familySpecialEntity){
        FamilySpecialHistoryEntity familySpecialHistoryEntity = new FamilySpecialHistoryEntity();

        BeanUtils.copyProperties(familySpecialEntity, familySpecialHistoryEntity);

        familySpecialHistoryEntity.setId(Constant.getUUID());

        familySpecialHistoryEntity.setOrderId(orderEntity.getOrderId());
        return familySpecialHistoryEntity;
    }


    public static FamilySupportHistoryEntity convertFamilySupportHistoryEntity(OrderEntity orderEntity,
                                                                              FamilySupportEntity familySupportEntity){
        FamilySupportHistoryEntity familySupportHistoryEntity = new FamilySupportHistoryEntity();

        BeanUtils.copyProperties(familySupportEntity, familySupportHistoryEntity);

        familySupportHistoryEntity.setId(Constant.getUUID());

        familySupportHistoryEntity.setOrderId(orderEntity.getOrderId());
        return familySupportHistoryEntity;
    }


    public static TmpFamilyMainHistoryEntity convertTmpFamilyMainHistoryEntity(TmpOrderEntity orderEntity, TmpFamilyMainEntity familyMainEntity,
                                                                               UserAccountVo userAccountVo){
        TmpFamilyMainHistoryEntity entity = new TmpFamilyMainHistoryEntity();

        BeanUtils.copyProperties(familyMainEntity, entity);

        entity.setId(Constant.getUUID());

        entity.setOrderId(orderEntity.getId());

        entity.setFamilyId(familyMainEntity.getId());





        entity.setOrgId(userAccountVo.getOrgId());

        //entity.setUpdateBy(userAccountVo.getUserId());



        entity.setCreateBy(userAccountVo.getUserId());

        entity.setCreateTime(new Date());

        return entity;
    }


    public static TmpFamilyMemberHistoryEntity convertTmpFamilyMemberHistoryEntity(TmpOrderEntity orderEntity,
                                                                                   TmpFamilyMemberEntity familyMemberEntity, TmpFamilyMainEntity familyMainEntity,
                                                                                   UserAccountVo userAccountVo){
        TmpFamilyMemberHistoryEntity entity = new TmpFamilyMemberHistoryEntity();

        BeanUtils.copyProperties(familyMemberEntity, entity);

        entity.setId(Constant.getUUID());

        entity.setOrderId(orderEntity.getId());

        entity.setFamilyId(orderEntity.getFamilyId());

        entity.setAddress(familyMainEntity.getAddress());

        entity.setMemberId(familyMemberEntity.getId());

        entity.setCreateBy(userAccountVo.getUserId());

        entity.setCreateTime(new Date());

        entity.setIsSaveObject(familyMemberEntity.getIsSaveObject());
        entity.setHelpPoorFlag(familyMemberEntity.getHelpPoorFlag());
        entity.setRuralObjectType(familyMemberEntity.getRuralObjectType());

        return entity;
    }
}
